using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_category_view_cateogory : System.Web.UI.Page
{
    private string id = "";
    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["user"];
        if (user_present == null)
        {
            Response.Redirect("../main/login.aspx");
        }

        id = Request["id"];

        if (Page.IsPostBack == false)
        {
            if (id == "new")
            {
                btnUpdate.ImageUrl = "../images/create.gif";
            }
            else
            {
                btnUpdate.ImageUrl = "../images/update.gif";
                BindData();
            }
        }

    }

    protected void BindData()
    {
        Category category = Category.get_category(Convert.ToInt32(id));
        txtName.Text = category.CategoryName;
        txtDescription.Text = category.CategoryDescription;
        fckFeature.Value = category.CategoryFeature;
        txturl.Text = category.Url;
    }

    protected void execute(object sender, EventArgs e)
    {
        int sequence = 0;
        if (txturl.Text == "") txturl.Text = "#";
        try
        {
            sequence = Convert.ToInt32(txtSequence); 
        }
        catch (Exception ex)
        {
            sequence = 1;
        }

        if (id == "new")
        {
            try
            {
                Category.create_category(txtName.Text, txturl.Text, txtDescription.Text, fckFeature.Value, sequence, 0, true, user_present);
                Response.Redirect("list_category.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
        else
        {
            try
            {
                Category.update_category(Convert.ToInt32(id), txtName.Text, txturl.Text, txtDescription.Text, fckFeature.Value, sequence, true, user_present);
                Response.Redirect("list_category.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }
}
